home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_Tix.idb / usr / freeware / lib / tix4.1 / demos / samples / DynTree.tcl.z / DynTree.tcl
Encoding:
Text File  |  1999-01-26  |  3.5 KB  |  146 lines

  1. # Tix Demostration Program
  2. #
  3. # This sample program is structured in such a way so that it can be
  4. # executed from the Tix demo program "widget": it must have a
  5. # procedure called "RunSample". It should also have the "if" statment
  6. # at the end of this file so that it can be run as a standalone
  7. # program using tixwish.
  8.  
  9. # This file demonstrates how to use the TixTree widget to display
  10. # dynamic hierachical data (the files in the Unix file system)
  11. #
  12.  
  13. proc RunSample {w} {
  14.  
  15.     # We create the frame and the ScrolledHList widget
  16.     # at the top of the dialog box
  17.     #
  18.     frame $w.top -relief raised -bd 1
  19.  
  20.     # Create a TixTree widget to display the hypothetical DOS disk drive
  21.     # 
  22.     #
  23.     tixTree $w.top.a  -options {
  24.     hlist.separator "/"
  25.     hlist.width 35
  26.     hlist.height 25
  27.     }
  28.  
  29.     pack $w.top.a -expand yes -fill both -padx 10 -pady 10 -side left
  30.  
  31.     set tree $w.top.a 
  32.     set hlist [$tree subwidget hlist]
  33.  
  34.     $tree config -opencmd "DynTree:OpenDir $tree"
  35.  
  36.     # Add the root directory the TixTree widget
  37.     DynTree:AddDir $tree /
  38.  
  39.     # The / directory is added in the "open" mode. The user can open it
  40.     # and then browse its subdirectories ...
  41.     
  42.  
  43.     # Use a ButtonBox to hold the buttons.
  44.     #
  45.     tixButtonBox $w.box -orientation horizontal
  46.     $w.box add ok     -text Ok     -underline 0 -command "destroy $w" \
  47.     -width 6
  48.     $w.box add cancel -text Cancel -underline 0 -command "destroy $w" \
  49.     -width 6
  50.  
  51.     pack $w.box -side bottom -fill x
  52.     pack $w.top -side top -fill both -expand yes
  53. }
  54.  
  55. proc DynTree:AddDir {tree dir} {
  56.     set hlist [$tree subwidget hlist]
  57.  
  58.     if {$dir == "/"} {
  59.     set text /
  60.     } else {
  61.     set text [file tail $dir]
  62.     }
  63.  
  64.     $hlist add $dir -itemtype imagetext \
  65.     -text $text -image [tix getimage folder]
  66.  
  67.     catch {
  68.     # We need a catch here because the directory may not be readable by us
  69.     #
  70.     $tree setmode $dir none
  71.     if {[glob -nocomplain $dir/*] != {}} {
  72.         $tree setmode $dir open
  73.     }
  74.     }
  75. }
  76.  
  77.  
  78. # This command is called whenever the user presses the (+) indicator or
  79. # double clicks on a directory whose mode is "open". It loads the files
  80. # inside that directory into the Tree widget.
  81. #
  82. # Note we didn't specify the -closecmd option for the Tree widget, so it
  83. # performs the default action when the user presses the (-) indicator or
  84. # double clicks on a directory whose mode is "close": hide all of its child
  85. # entries
  86. #
  87. proc DynTree:OpenDir {tree dir} {
  88.     set PWD [pwd]
  89.     set hlist [$tree subwidget hlist]
  90.  
  91.     if {[$hlist info children $dir] != {}} {
  92.     # We have already loaded this directory. Let's just
  93.     # show all the child entries
  94.     #
  95.     # Note: since we load the directory only once, it will not be
  96.     #       refreshed if the you add or remove files from this
  97.     #    directory.
  98.     #
  99.     foreach kid [$hlist info children $dir] {
  100.         $hlist show entry $kid
  101.     }
  102.     return
  103.     }
  104.  
  105.     if [catch {cd $dir}] {
  106.     # We can't read that directory, better not do anything
  107.     cd $PWD
  108.     return
  109.     }
  110.  
  111.     set files [lsort [glob -nocomplain *]]
  112.     foreach f $files {
  113.     if [file isdirectory $f] {
  114.         if {$dir == "/"} {
  115.         set subdir /$f
  116.         } else {
  117.         set subdir $dir/$f
  118.         }
  119.         DynTree:AddDir $tree $subdir
  120.     } else {
  121.         if {$dir == "/"} {
  122.         set file /$f
  123.         } else {
  124.         set file $dir/$f
  125.         }
  126.  
  127.         $hlist add $file -itemtype imagetext \
  128.         -text $f -image [tix getimage file]
  129.     }
  130.     }
  131.  
  132.     cd $PWD
  133. }
  134.  
  135. # This "if" statement makes it possible to run this script file inside or
  136. # outside of the main demo program "widget".
  137. #
  138. if {![info exists tix_demo_running]} {
  139.     wm withdraw .
  140.     set w .demo
  141.     toplevel $w
  142.     RunSample $w
  143.     bind .demo <Destroy> exit
  144. }
  145.  
  146.